package ua.com.toltec.flymodemanager;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;

public class PreferenceUnit {
	Context mContext;
	SharedPreferences prefs;

	public PreferenceUnit(Context mContext) {
		super();
		this.mContext = mContext;
		this.prefs = PreferenceManager
				.getDefaultSharedPreferences(this.mContext);
	}

	public static long intervalToNextTime(Date today, int hour, int minute) {
		Calendar calendar = Calendar.getInstance();
		GregorianCalendar d1 = new GregorianCalendar(
				calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
				calendar.get(Calendar.DAY_OF_MONTH),
				calendar.get(Calendar.HOUR_OF_DAY),
				calendar.get(Calendar.MINUTE));
		// if next day add one day
		if ((calendar.get(Calendar.HOUR_OF_DAY) > hour)
				|| ((calendar.get(Calendar.HOUR_OF_DAY) == hour) && (calendar
						.get(Calendar.MINUTE) >= minute))) {
			d1.add(Calendar.DATE, 1);
		}
		d1.set(Calendar.HOUR_OF_DAY, hour);
		d1.set(Calendar.MINUTE, minute);
		d1.set(Calendar.SECOND, 0);
		d1.set(Calendar.MILLISECOND, 0);
		return d1.getTimeInMillis() - today.getTime();
	}

	public Boolean isServiceStart() {
		return prefs.getBoolean(Constants.STARTSERVICE, false);
	}

	public String getCurentTask() {
		GregorianCalendar curentDate = new GregorianCalendar();

		GregorianCalendar fOn = getCurentDateTask(curentDate,
				prefs.getString(Constants.FLYMODEON, "00:00"));
		GregorianCalendar fOff = getCurentDateTask(curentDate,
				prefs.getString(Constants.FLYMODEOFF, "00:00"));

		if (curentDate.equals(fOff)) {
			return Constants.FLYMODEOFF;
		} else if (curentDate.equals(fOn)) {
			return Constants.FLYMODEON;
		}
		return "";
	}

	public static GregorianCalendar getCurentDateTask(
			GregorianCalendar calendar, String strTime) {

		GregorianCalendar calendarToday = (GregorianCalendar) calendar.clone();

		int hour = getHour(strTime);
		int minute = getMinute(strTime);

		calendarToday.set(Calendar.HOUR_OF_DAY, hour);
		calendarToday.set(Calendar.MINUTE, minute);

		return calendarToday;
	}

	// ----------------------------------------------------------------
	public long getNextInterval() {
		GregorianCalendar calendar = new GregorianCalendar();
		return getNextInterval(calendar);
	}

	// ----------------------------------------------------------------
	public long getNextInterval(GregorianCalendar calendar) {

		long fOn = getDateNextTask(calendar,
				prefs.getString(Constants.FLYMODEON, "00:00"));
		long fOff = getDateNextTask(calendar,
				prefs.getString(Constants.FLYMODEOFF, "00:00"));

		Log.i("getNextInterval", Long.toString(Math.min(fOn, fOff)));
		Log.i("getNextInterval", prefs.getString(Constants.FLYMODEON, "00:00"));
		Log.i("getNextInterval", Long.toString(fOn));
		Log.i("getNextInterval", prefs.getString(Constants.FLYMODEOFF, "00:00"));
		Log.i("getNextInterval", Long.toString(fOff));

		return Math.min(fOn, fOff);
	}

	// ----------------------------------------------------------------
	protected static int getHour(String strTime) {
		try {
			return Math.min(23, Integer.parseInt(strTime.substring(0, 2)));
		} catch (Exception e) {
			return 0;
		}
	}

	// ----------------------------------------------------------------
	protected static int getMinute(String strTime) {
		try {
			return Math.min(59, Integer.parseInt(strTime.substring(3, 5)));
		} catch (Exception e) {
			return 0;
		}

	}

	// ----------------------------------------------------------------
	public static long getDateNextTask(String strTime) {
		GregorianCalendar calendarToday = new GregorianCalendar();
		return getDateNextTask(calendarToday, getHour(strTime),
				getMinute(strTime));

	}

	// ----------------------------------------------------------------
	public static long getDateNextTask(GregorianCalendar calendarToday,
			String strTime) {
		return getDateNextTask(calendarToday, getHour(strTime),
				getMinute(strTime));
	}

	// ----------------------------------------------------------------
	public static long getDateNextTask(GregorianCalendar calendar, int hour,
			int minute) {
		GregorianCalendar calendarToday = (GregorianCalendar) calendar.clone();
		// if next day add one day
		if ((calendarToday.get(Calendar.HOUR_OF_DAY) > hour)
				|| ((calendarToday.get(Calendar.HOUR_OF_DAY) == hour) && (calendarToday
						.get(Calendar.MINUTE) >= minute))) {
			calendarToday.add(Calendar.DATE, 1);
		}
		calendarToday.set(Calendar.HOUR_OF_DAY, hour);
		calendarToday.set(Calendar.MINUTE, minute);
		calendarToday.set(Calendar.SECOND, 0);
		calendarToday.set(Calendar.MILLISECOND, 0);
		// Log.i("getDate ",calendarToday.toString());
		// Log.i("getDate",d1.toString());
		return calendarToday.getTimeInMillis();
	}
	
	public Calendar milliseconds2calendar(Long milliseconds) {
		Timestamp t = new Timestamp(milliseconds); // replace with existing timestamp
        Date d = new Date(t.getTime());
        Calendar gregorianCalendar = GregorianCalendar.getInstance();
        gregorianCalendar.setTime(d);
		return gregorianCalendar;
	}
	
	public String calendar2string(Calendar calendar) {
		DateFormat df = new SimpleDateFormat("dd/MM/yy HH:mm:ss");
		return  df.format(calendar.getTime());
	}

}
